package com.zr.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class LngAndLatUtil {

    public static Map<String,Double> getDistance(String departure,String destination) {
        Map<String,Double> start = LngAndLatUtil.getLngAndLat(departure);
        Map<String,Double> end = LngAndLatUtil.getLngAndLat(destination);
        Map<String,Double> result = getDistanceByGaode(start.get("lng"),start.get("lat"),end.get("lng"),end.get("lat"));
        return result;
    }

    public static Map<String,Double> getLngAndLat(String address){
        Map<String,Double> map=new HashMap<String, Double>();
//        String url = "https://api.map.baidu.com/geocoding/v3/?address="+address+"&output=json&ak=VnaD0VGkUnBHi80BfYaXl3G4LRgs3Gc7&callback=showLocation";
        String url = "https://api.map.baidu.com/geocoding/v3/?address="+address+"&output=json&ak=hmVGjEdOEvriWYdNlazgirHUMMFWaeys&callback=showLocation";

        String json = loadJSON(url);
        String json2 = json.substring(27);
        String json3 = json2.substring(0,json2.length()-1);
//        System.out.println(json3);
        JSONObject obj = JSON.parseObject(json3);
        if(obj.get("status").toString().equals("0")){
            double lng=obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
            DecimalFormat df = new DecimalFormat("0.0000");
            double lat=obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
            map.put("lng", Double.parseDouble(df.format(lng)));
            map.put("lat", Double.parseDouble(df.format(lat)));
        }
        return map;
    }

    public static Map<String,Double> getDistanceByGaode(double startLng,double startLat,double endLng,double endLat){
        Map<String,Double> map=new HashMap<String, Double>();
        String url = "https://restapi.amap.com/v3/distance?origins="+startLng+","+startLat+"&destination="+endLng+","+endLat+"&key=870a2505da2303f0fd87c9eb66519f01&output=json、";
        String json = loadJSON(url);
        JSONObject obj = JSON.parseObject(json);
        System.out.println(obj.toString());
        if(obj.get("status").toString().equals("1")){
            JSONArray jsonArray = obj.getJSONArray("results");
            JSONObject objectInArray = jsonArray.getJSONObject(0);
            double distance = objectInArray.getDouble("distance");
            double duration = objectInArray.getDouble("duration");
            map.put("distance", distance);
            map.put("duration", duration);
        }
        return map;
    }
    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }


}
